package leeon.mobile.BBSBrowser;

import java.util.ArrayList;
import java.util.List;

import leeon.kaixin.wap.action.RepasteAction;
import leeon.kaixin.wap.models.Repaste;
import leeon.kaixin.wap.models.RepasteType;
import leeon.mobile.BBSBrowser.actions.ContentException;
import leeon.mobile.BBSBrowser.actions.NetworkException;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class KaixinRepasteActivity extends ListActivity {
	
	private List<Repaste> list;
	private String uid;
	private String uname;
	private String verify;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		super.onCreate(savedInstanceState);
		
		uid = this.getIntent().getStringExtra("uid");
		uname = this.getIntent().getStringExtra("uname");
		verify = KaixinMainActivity.verify;
		
		list = new ArrayList<Repaste>();
		getListView().setBackgroundColor(Color.WHITE); 
		getListView().setDivider(getResources().getDrawable(R.drawable.kaixin_divider));
		setListAdapter(new RepasteListAdapter(list));
		scrollAction.addActionWhenScroll(getListView());
				
		if (verify != null) {
			scrollAction.refresh();
		}
		
		if (customTitleSupported) {
			getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.kaixin_titlebar);
			TextView title = (TextView)findViewById(R.id.kaixinTitle);
			title.setText(uname + "给大家的转帖");
		}
	}
	
	@Override 
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, final int position, long id) {
		Repaste r = list.get(position);
		KaixinRepasteActivity.this.startActivity(
				new Intent(KaixinRepasteActivity.this, KaixinRepasteDetailActivity.class)
				.putExtra("repaste", r)
				.putExtra("uname", uname));
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 1, "刷新动态").setIcon(android.R.drawable.ic_menu_rotate);
        return result;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			scrollAction.refresh();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void listRepaste(final int totalItemCount) {
		UIUtil.runActionInThread(this, new UIUtil.ActionInThread<Repaste>() {
			@Override
			public void action() throws NetworkException, ContentException {
				if (verify != null && uid != null) {
					if (list.size() == 0)
						bglist = RepasteAction.listRepaste(verify, uid);
					else
						bglist = RepasteAction.listRepaste(verify, uid, totalItemCount);
				}
			}
			@Override
			public void actionFinish() {
				if (bglist != null) {
					list.addAll(bglist);
					((BaseAdapter)KaixinRepasteActivity.this.getListView().getAdapter()).notifyDataSetChanged();
				}
			}
			
			@Override
			public void actionError() {
				scrollAction.rollback();
			}
		});
	}
	
	private KaixinListScrollAction scrollAction = new KaixinListScrollAction();
	private class KaixinListScrollAction extends UIUtil.ActionInScroll {
		
		public KaixinListScrollAction() {
			new UIUtil().super();
		}
		
		@Override
		protected void actionClear() {
			list.clear();
		}

		@Override
		protected void actionScroll(int totalItemCount) {
			listRepaste(totalItemCount);
		}

		@Override
		protected void actionRemove(Object o) {
			list.remove(o);
		}
		
		@Override
		protected int pageSize() {
			return 10;
		}
		
	}
	
	private class RepasteListAdapter extends BaseAdapter {

		private List<Repaste> list;

		public RepasteListAdapter(List<Repaste> list) {
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public Object getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View tv;
			if (convertView == null) {
				tv = LayoutInflater.from(KaixinRepasteActivity.this).inflate(android.R.layout.simple_list_item_2, parent, false);
			} else {
				tv = convertView;
			}
			
			TextView text1 = (TextView)tv.findViewById(android.R.id.text1);
			TextView text2 = (TextView)tv.findViewById(android.R.id.text2);
			
			Repaste r = list.get(position);
			text1.setText(r.getTitle());
			text1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
			text1.setTextColor(Color.DKGRAY);
			text2.setText(r.getDate());
			
			if (RepasteType.日记.equals(r.getType()))
				text1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.repost_diary, 0, 0, 0);
			else if (RepasteType.网页.equals(r.getType()))
				text1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.repost_repastedefault, 0, 0, 0);
			else
				text1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.repost_othervideo, 0, 0, 0);
			
			if (position%2 == 1) {
				tv.setBackgroundResource(R.drawable.kaixin_selector1);
			} else {
				tv.setBackgroundResource(R.drawable.kaixin_selector2);
			}
			
			return tv;
		}		
	}

}
